package com.itheima.web.controller;

import com.itheima.domain.system.User;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected HttpSession session;


    protected String getCompanyId() {
        return getLoginUser().getCompanyId();
    }

    protected String getCompanyName() {
        return getLoginUser().getCompanyName();
    }

    //获取登录人
    protected User getLoginUser() {
         User loginUser = (User)session.getAttribute("loginUser");
         if (loginUser == null){
             try {
                 request.setAttribute("error", "当前用户信息已经失效,请重新登录");
                 request.getRequestDispatcher("/login.jsp").forward(request,response);
             } catch (Exception e) {
                 e.printStackTrace();
                 throw new RuntimeException();
             }
         }
         return loginUser;
    }
}
